// fs.readFile('./1.txt', (error, data) => {
//     if (error) {
//         console.log('失败')
//         return
//     }
//     console.log(data.toString())  // 成功
// })

// ajax('get', './1.json', {
//     success: () => { },
//     fail: () => { }
// })


ajax = (method, url, options) => {
    const { success, fail } = options // 析构赋值
    // const success = options.success
    // const fail = options.fail
    const request = new XMLHttpRequest()
    request.open(method, url)
    request.onreadystatechange = () => {
        if (request.readyState === 4) {
            // 成功就调用success，失败就调用success
            if (request.status < 400) {
                success.call(null, request.response)
            } else if (request.status >= 400) {
                fail.call(null, request, request.status)
            }
        }
    }
    request.send()
}

ajax('get', '/xxx', {
    success(response) { }, fail: (request, status) => { }
})  // 左边为function 缩写, 原为success: function(response){}， 右边为箭头函数